<script setup lang="ts" name="BigScreenGroupAddEdit">
import { bigScreen } from '@/api/bigScreen'
import { successMessage, errorMessage } from '@/utils/message'
const AddEditFormRef = ref(null)

const emit = defineEmits(['save'])

const props = defineProps({
  groupList: { type: Array as any }
})

const visible = ref(false)
const title = ref('')
const expandNodes = ref([0])
const formData = ref({
  id: 0,
  title: '',
  parentId: 0,
  sort: 0
})
const rules = ref({
  title: { required: true, message: '分组名称不能为空', trigger: ['blur', 'change'] }
})

// 显示新建/编辑部门弹窗
const showDialog = (v) => {
  visible.value = true
  if (v) {
    title.value = '编辑分组'
    formData.value.id = v.id
    formData.value.title = v.title
    formData.value.parentId = v.parentId
    formData.value.sort = v.sort
  } else {
    title.value = '新增分组'
  }
}
// 子组件将方法暴露给父组件
defineExpose({ showDialog })
const closeDialog = () => {
  AddEditFormRef.value.resetFields()
  formData.value = {
    id: 0,
    title: '',
    parentId: 0,
    sort: 0
  }
  visible.value = false
}

const handleOk = () => {
  if (!AddEditFormRef.value) return false
  AddEditFormRef.value.validate((valid) => {
    if (valid) {
      const result: any = {}
      result.title = formData.value.title
      result.parentId = formData.value.parentId
      result.sort = formData.value.sort
      result.id = formData.value.id
      if (formData.value.id) {
        bigScreen.group.edit(result, { config: { showLoading: false, mockEnable: true } }).then((res: any) => {
          console.log(res)
          if (res.code === 200) {
            successMessage('大屏分组编辑成功！')
            emit('save')
            closeDialog()
          } else {
            errorMessage(res.info)
          }
        })
      } else {
        bigScreen.group.add(result, { config: { showLoading: false, mockEnable: true } }).then((res: any) => {
          console.log(res)
          if (res.code === 200) {
            successMessage('大屏分组添加成功！')
            emit('save')
            closeDialog()
          } else {
            errorMessage(res.info)
          }
        })
      }
    }
  })
}
</script>

<template>
  <el-dialog v-model="visible" width="600px" append-to-body destroy-on-close :title="title">
    <el-form ref="AddEditFormRef" label-width="auto" label-position="right" :model="formData" :rules="rules">
      <el-form-item label="父级分组">
        <el-tree-select v-model="formData.parentId" :data="props.groupList" :default-expanded-keys="expandNodes" node-key="id" :props="{ label: 'title' }" check-on-click-node check-strictly filterable placeholder="请选择父级分组" />
      </el-form-item>
      <el-form-item label="分组名称" prop="title">
        <el-input v-model="formData.title" clearable placeholder="请输入分组名称" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="formData.sort" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="modal-footer" ref="modalFooter">
        <el-button @click="closeDialog">{{ $t('buttons.buttonCancel') }}</el-button>
        <el-button type="primary" @click="handleOk">{{ $t('buttons.buttonConfirm') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.el-form {
  padding: 40px 80px;
}

.modal-footer {
  display: inline-flex;
  overflow: hidden;
}
</style>
